let ip_local = null;//定义本地IP变量
let address = null;//定义解析IP后的地址
function getChinaToday() {
    $.ajax({
        type: 'get',
        url: '/getChinaDay',
        data: {},
        success: function (data) {
            console.log(typeof data);
            let ctext = '较上日变化：';
            $("#TConfirm").text(data['confirm']);
            $("#TSuspect").text(data['suspect']);
            $("#THeal").text(data['heal']);
            $("#TDead").text(data['dead']);
            $("#TnoInfect").text(data['noInfect']);
            $("#Tnowconfirm").text(data['nowconfirm']);
            $("#lastTime").text('最近更新时间：' + data['lastUpdateTime']);
            $("#TConfirmC").text(data['confirmCompare']).prepend(ctext);
            $("#TSuspectC").text(data['suspectCompare']).prepend(ctext);
            $("#THealC").text(data['healCompare']).prepend(ctext);
            $("#TDeadC").text(data['deadCompare']).prepend(ctext);
            $("#TnoInfectC").text(data['noInfectCompare']).prepend(ctext);
            $("#TnowconfirmC").text(data['nowconfirmCompare']).prepend(ctext);
        }, error: function () {
            console.log("这个接口挂了");
        }
    })
}

function getCityInfo(pname, cname) {
    $.ajax({
        type: 'get',
        url: '/city',
        data: {pname:pname,cname:cname},
        success: function (data) {
            console.log(data);
            if(data['cdata']['confirm'][0] == null){
                $("#localinfor").text('暂未获取到您的具体位置信息,无法获悉您当前地理位置情况');
                window.setTimeout(function (){
                    $("#local").hide();
                },2000);
            }else{
              $("#localinfor").text('您当前地理位置为'+
                                address['data']['region']+address['data']['city']+'，当地截止数据最近更新时间累计确诊'
                                +data['cdata']['confirm'][0]['value']+
                '人,现存确诊'+data['cdata']['nowconfirm'][0]['value']+'人,已治愈'+data['cdata']['heal'][0]['value']
                +'人（地理位置仅供参考）');
            }

        }, error: function () {
            console.log("这个接口挂了");
        }
    })
}

function getIp() {//获取客户端ip
    $.ajax({
        type: 'GET',
        url: 'https://api.ipify.org/?format=jsonp&callback=jQuery110202935515424244193_1618314647280&_=1618314647281',
        success: function (localIP) {
            let ip_str = localIP.match(/\((.+?)\)/g)[0].replace("(",'').replace(")",'');
            ip_local = JSON.parse(ip_str);
            //console.log(ip_res);
        }, error: function () {
            console.log("这个接口挂了");
        }
    });
}

function getIpInfor(){//解析客户端IP
    $.ajax({
        type: 'POST',
        url: 'https://www.bejson.com/Bejson/Api/Ip/getIp',
        data: ip_local,
        success: function (cdata) {
            address = cdata;
            console.log(address);
            getCityInfo(address['data']['region'], address['data']['city'])
        }, error: function () {
            console.log("这个接口挂了");
        }
    });
}

$("#foot").click(function () {
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
});

getChinaToday();
getIp();
getIpInfor();